<!--
 * @ Author: hongjiazhun
 * @ Create Time: 2023-03-10 22:23:11
 * @ Modified by: hongjiazhun
 * @ Modified time: 2023-04-01 22:48:21
 * @ Description:排班详情
 -->

<template>
  <div class="app-container">
    <!-- @on-load="onLoad" @refresh-change="refreshChange"-->
    <avue-crud
      ref="crud"
      :search.sync="searchParams"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :option="option"
      :table-loading="loading"
      @selection-change="selectionChange"
      @size-change="sizeChange"
      @current-change="currentChange"
      @search-change="searchChange"
      @search-reset="resetChange"
    >
      <template slot="optUserName" slot-scope="{ row }">
        <DetailInfo :list="row.scheduleDeptDetailList"></DetailInfo>
      </template>
      <template slot-scope="scope" slot="menuLeft">
        <el-button
          circle
          icon="el-icon-document-copy"
          size="small"
          title="导出"
          @click="exportHandle"
        ></el-button>
      </template>
      <!-- <template slot-scope="{ row, type }" slot="menu">
        <el-button
          :type="type"
          icon="el-icon-view"
          size="small"
          @click="checkDetails(row)"
          >详情</el-button
        >
      </template> -->
    </avue-crud>

    <!-- <DetailList
      @close="closeDetailsModal"
      v-if="showDetails"
      :tableData="rowDetailsData"
    ></DetailList> -->
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getList, exportData } from "@/api/arrangeClasses/details";
import DetailInfo from "./components/DetailInfo.vue";

export default {
  name: "detail",
  components: {
    DetailInfo,
  },
  data() {
    return {
      showDetails: false,
      rowDetailsData: [],
      tableData: [],
      page: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
      },
      loading: false,
      // 选择
      selectionData: [],
      form: {},
      // 搜索参数
      searchParams: {},
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
    // 判断是否有查看全部的权限
    isCheckAll() {
      const { roles = [] } = this.userInfo;
      const ALL = ["管理员", "超级管理员", "临时管理员"];
      const info = roles.find((item) => ALL.includes(item.roleName)) || {};
      return Object.keys(info).length !== 0;
    },
    option() {
      return {
        border: true,
        columnBtn: false,
        delBtn: false,
        editBtn: false,
        addBtn: false,
        refreshBtn: false,
        // selection: true,
        tip: false,
        // dialogDrag: true,
        searchMenuSpan: 4,
        menu: false,
        column: [
          {
            label: "排班日期",
            prop: "scheduleDay",
            search: true,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
          },
          {
            label: "单位代码",
            prop: "deptId",
            search: this.isCheckAll,
            hide: true,
          },
          {
            label: "单位名称",
            prop: "deptName",
          },
          {
            label: "带班领导",
            prop: "leaderName",
            searchSpan: 5,
          },
          {
            label: "办公电话",
            prop: "leaderPhone",
          },

          {
            label: "手机号",
            prop: "optUserPhone",
          },

          {
            label: "排班详情",
            prop: "optUserName",
            slot: true,
            width: 600,
          },
        ],
      };
    },
  },
  methods: {
    // 获取数据
    async onLoad() {
      this.loading = true;
      const { currentPage = 1, pageSize } = this.page;
      const { deptId } = this.userInfo;
      // 设置请求参数
      const params = {
        pageNum: currentPage,
        pageSize,
        deptId,
        ...this.searchParams,
      };
      const { code, rows = [], total } = await getList(params);
      this.tableData = code === 200 ? rows : [];
      this.loading = false;
      // 设置总条数
      this.page.total = total;
    },
    // 刷新
    refreshChange() {
      this.onLoad();
    },
    // 条数
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    // 页数
    currentChange(val) {
      this.page.currentPage = val;
    },
    resetChange(item) {
      // this.onLoad();
      this.tableData = [];
    },
    searchChange(params, done) {
      if (Object.keys(params).length === 0) {
        done();
        this.$message.error("请输入搜索条件查询!");
        return;
      }
      this.onLoad();
      done();
    },
    // 多选
    selectionChange(selection) {
      this.selectionData = selection;
    },
    // 查看详情
    checkDetails(row) {
      const { scheduleDeptDetailList = [] } = row;
      this.rowDetailsData = scheduleDeptDetailList;
      this.showDetails = true;
    },
    // 关闭
    closeDetailsModal() {
      this.showDetails = false;
    },
    // 导出
    async exportHandle() {
      const data = await exportData();
      const blob = new Blob([data], { type: "application/vnd.ms-excel" });
      let src = URL.createObjectURL(blob);
      if (src) {
        let a = document.createElement("a");
        a.style.display = "none";
        a.download = new Date().getTime();
        a.href = src;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(src);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
</style>